package com.gc.service.impl;

import com.gc.entity.*;
import com.gc.repository.*;
import com.gc.repository.impl.*;
import com.gc.service.CheckService;
import com.gc.service.StudentService;
import com.gc.service.TeachClassService;
import com.gc.vo.CheckShow;
import com.gc.vo.ShowClassByStudent;
import com.gc.vo.StuCheckShow;
import org.apache.poi.ss.formula.functions.T;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class StudentServiceImpl implements StudentService {
    StudentRepository studentRepository = new StudentRepositoryImpl();
    CourseRepository courseRepository = new CourseRepositoryImpl();
    TeachClassRepository teachClassRepository = new TeachClassRepositoryImpl();
    CcsRepository ccsRepository = new CcsRepositoryImpl();
    TextBookRepository textBookRepository = new TextBookRepositoryImpl();
    StuCheckRepository stuCheckRepository = new StuCheckRepositoryImpl();
    CheckRepository checkRepository = new CheckRepositoryImpl();
    @Override
    public boolean Add(Student student) {
        return studentRepository.Add(student);
    }

    @Override
    public List<Student> FindAll() {
        return studentRepository.FindAll();
    }

    @Override
    public boolean DelById(Long id) {
        Student student = studentRepository.FindById(id);
        List<Ccs> byStudentId = ccsRepository.findByStudentId(student.getSid());
        if(byStudentId != null) {
            ccsRepository.DelByStudentId(student.getSid());
        }
        studentRepository.DelById(id);
        return true;
    }

    @Override
    public Student FindById(Long id) {
        return studentRepository.FindById(id);
    }

    @Override
    public boolean UpdateById(Student student) {
        return studentRepository.UpdateById(student);
    }


    @Override
    public List<ShowClassByStudent> DataById(Long id) {
        Student students = studentRepository.FindById(id);
        List<Ccs> ccs = ccsRepository.findByStudentId(students.getSid());
        List<ShowClassByStudent> res = new ArrayList<>();
        for(Ccs i : ccs) {
            TeachClass teachClass = teachClassRepository.FindByClassId(i.getClassid());
            Course course = courseRepository.FindByCourseId(teachClass.getCid());
            res.add(new ShowClassByStudent(
                    teachClass.getName(),
                    teachClass.getClassid(),
                    course.getName(),
                    course.getCid()
            ));
        }
        return res;
    }
//    String sid;
//    String checkid;
//    Integer stage;
//    String bookid;
//    String bookname;
//    BigDecimal price;
//    String cid;
//    String coursename;
//    Timestamp starttime;
//    Timestamp endtime;
    @Override
    public List<StuCheckShow> StuCheckById(Long id) {
        Student students = studentRepository.FindById(id);
        List<StuCheck> stuChecks = stuCheckRepository.FindByStudentId(students.getSid());
        List<StuCheckShow> res = new ArrayList<>();
        stuChecks.forEach( item -> {
            Check check = checkRepository.FindByCheckId(item.getCheckid());
            TextBook textBook = textBookRepository.FindByBookId(check.getBookid());
            Course course = courseRepository.FindByCourseId(check.getCid());
            res.add(new StuCheckShow(
                    item.getId(),
                    item.getSid(),
                    item.getCheckid(),
                    item.getStage(),
                    textBook.getBookid(),
                    textBook.getName(),
                    textBook.getPrice(),
                    course.getCid(),
                    course.getName(),
                    check.getStarttime(),
                    check.getEndtime()
                ));
            }
        );
        return res;
    }
}
